﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PDAMain.DAL;
using PDAMain.Entity;
using System.Runtime.InteropServices;
using PDAMain.Interface;

namespace PDAMain.Forms.ReadingWatermeter
{
    public partial class WaterReadingList : BaseForm
    {
        private string strBarCode="";
        public WaterReadingList()
        {
            InitializeComponent();
        }




        private void BindData()
        {
            DataSet ds = DataHelper.ReadingWatermeterDao.list(); 
           if (ds != null && ds.Tables.Count >0 && ds.Tables[0].Rows.Count > 0)
           {
              // InsertWaterMeterData(ds);

               DataTable dtmapping = ds.Tables[0];
               WaterMList.DataSource = dtmapping;

               DataGridTableStyle dts = new DataGridTableStyle();
               dts.MappingName = dtmapping.TableName;

               DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
               col1.HeaderText = "资产条码";
               col1.MappingName = "BARCODE";
               col1.Width = 185;
               dts.GridColumnStyles.Add(col1);

               DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
               col2.HeaderText = "抄表月份";
               col2.MappingName = "READMONTH";
               col2.Width = 64;
               dts.GridColumnStyles.Add(col2);

               DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
               col3.HeaderText = "上月读数";
               col3.MappingName = "PRE_DEGREE";
               col3.Width = 65;
               dts.GridColumnStyles.Add(col3);

               WaterMList.TableStyles.Clear();
               WaterMList.TableStyles.Add(dts);
               WaterMList.Refresh(); 
           }
        }


 
       
        /// <summary>
        /// 跳转详情页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
 
        private void WaterMList_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)WaterMList.DataSource;
            if(dt != null && dt.Rows.Count >0)
            { 
                if (dt.Rows[WaterMList.CurrentRowIndex]["READDEGREE"].ToString() == "0")
                {
                    DataHelper.ReadingWatermeterDao.Wm_Meter_Id = dt.Rows[WaterMList.CurrentRowIndex]["WM_METER_ID"].ToString();
                    DataHelper.ReadingWatermeterDao.MprId = dt.Rows[WaterMList.CurrentRowIndex]["MPRId"].ToString();
                }
            }
        }

        private void WaterReadingList_Load(object sender, EventArgs e)
        {
            try
            {
                CloseBarCode();
                // 打开BarCode扫描设备
                if (!OpenBarCode())
                {
                    MessageBox.Show("打开扫描接口失败，请重登系统");
                }

                EnableBeeper(true);
                EnableVibrater(true);
                BindData();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        // 重载 基类BaseForm的这个方法
        public override void OnBarCodeNotify(byte[] BarCodeData, int nLength)
        {
            EnableBarCode(false);

            string BarCode = Encoding.Default.GetString(BarCodeData, 0, nLength);
            strBarCode = BarCode;
            EnableBarCode(true);
            bool Flag=MearchData(strBarCode);
            if (Flag)
            {
               WaterReadingDetail WRD = new WaterReadingDetail();
               WRD.Show();
                this.Close();
                CloseBarCode();
            }
        }

        private void WaterReadingList_Closed(object sender, EventArgs e)
        {
            // 关闭BarCode扫描设备
            CloseBarCode();
        }
        private bool MearchData(string strBC)
        {
            bool Flag = false;
            DataTable dt = (DataTable)WaterMList.DataSource;
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt.Rows[i]["BARCODE"].ToString() == strBarCode && dt.Rows[i]["READDEGREE"].ToString() == "0")
                    {

                        DataHelper.ReadingWatermeterDao.Wm_Meter_Id = strBarCode;
                        DataHelper.ReadingWatermeterDao.MprId = dt.Rows[i]["MPRId"].ToString(); 
                        return true;
                    }
                }

            }
            return Flag;
        }
        private void menuBack_Click(object sender, EventArgs e)
        {
            WaterPlanList wpl = new WaterPlanList();
            wpl.Show();
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                BindData();
            }
            catch
            {
                //timer1.Enabled = false;
                //MessageBox.Show("数据同步异常");
            }
        }
    }
}